#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "wayPoint.h"
#include"TowerPosition.h"
#include <QTimer>
#include "enemy.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow), m_playerHp(5)
    ,m_playerGlod(1000),m_waves(0)
    ,m_gameWin(false)
    ,m_gameLose(false)
{
    ui->setupUi(this);
    addWayPoint1();//增加航点函数的调用
    loadTowerPosition1();
    QTimer * timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(updateMap()));
    timer->start(30);
    QTimer::singleShot(300,this,SLOT(gameStart()));
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent * event)
{
    QPoint pressPos=event->pos();//得到鼠标点击的位置
    auto it=m_towerPositionList.begin();
    while(it!=m_towerPositionList.end())//遍历所有的防御塔坑
    {
        if(Qt::LeftButton==event->button())//如果是鼠标左键点击
        {
            if(it->ContainPos(pressPos) && !it->hasTower())//如果鼠标点击的位置在防御塔坑的范围内,并且没有防御塔
            {
                Tower * tower=new Tower(it->getCenterPos(),this);//创建一个新的防御塔
                m_towerList.push_back(tower);//把这个防御塔放到储存防御塔的list中
                it->setHasTower(true);//设置这个防御塔坑内有防御塔了
                update();//更新地图
                break;//进行了一次操作，可以直接退出循环了
            }
        }
        ++it;
    }
}
void MainWindow::addWayPoint1()
{
    wayPoint* waypoint1 = new wayPoint(QPoint(698, 206));
    m_wayPointList.push_back(waypoint1);

    wayPoint* waypoint2 = new wayPoint(QPoint(514, 203));
    waypoint1->setNextWayPoint(waypoint2);
    m_wayPointList.push_back(waypoint2);

    wayPoint* waypoint3 = new wayPoint(QPoint(510, 95));
    waypoint2->setNextWayPoint(waypoint3);
    m_wayPointList.push_back(waypoint3);

    wayPoint* waypoint4 = new wayPoint(QPoint(381, 95));
    waypoint3->setNextWayPoint(waypoint4);
    m_wayPointList.push_back(waypoint4);

    wayPoint* waypoint5 = new wayPoint(QPoint(354, 196));
    waypoint4->setNextWayPoint(waypoint5);
    m_wayPointList.push_back(waypoint5);

    wayPoint* waypoint6 = new wayPoint(QPoint(185, 227));
    waypoint5->setNextWayPoint(waypoint6);
    m_wayPointList.push_back(waypoint6);

    wayPoint* waypoint7 = new wayPoint(QPoint(189, 328));
    waypoint6->setNextWayPoint(waypoint7);
    m_wayPointList.push_back(waypoint7);

    wayPoint* waypoint8 = new wayPoint(QPoint(436, 323));
    waypoint7->setNextWayPoint(waypoint8);
    m_wayPointList.push_back(waypoint8);

    wayPoint* waypoint9 = new wayPoint(QPoint(422, 429));
    waypoint8->setNextWayPoint(waypoint9);
    m_wayPointList.push_back(waypoint9);

    wayPoint* waypoint10 = new wayPoint(QPoint(316, 455));
    waypoint9->setNextWayPoint(waypoint10);
    m_wayPointList.push_back(waypoint10);

    wayPoint* waypoint11 = new wayPoint(QPoint(312, 597));
    waypoint10->setNextWayPoint(waypoint11);
    m_wayPointList.push_back(waypoint11);
}
void MainWindow::paintEvent(QPaintEvent*)
{
    //添加在函数的前端
    if(m_gameLose || m_gameWin)//画出游戏结束的画面
        {
            QString text=m_gameLose ? "YOU LOST":"YOU WIN";
            QPainter painter(this);
            painter.setPen(Qt::red);
            painter.drawText(rect(),Qt::AlignCenter,text);
            return ;
        }

    QPainter painter(this);
    QString path(":/images/background1.bmp");//path是你图片的路径

    painter.drawPixmap(0,0,700,600,path);
    foreach(const wayPoint * waypoint,m_wayPointList)
            waypoint->draw(&painter);
    foreach(const TowerPosition towerposition,m_towerPositionList)
           towerposition.draw(&painter);
    foreach(const Tower * tower, m_towerList)
            tower->draw(&painter);
    //添加在函数的后端
        foreach(const Enemy * enemy,m_enemyList)
            enemy->draw(&painter);
    //drawPixmap的前四个参数代表的分别是，图片左上角的横坐标，图片左上角的纵坐标，图片的width，图片的height。我们一般把width和height，与图片的真实大小匹配起来
}



void MainWindow::loadTowerPosition1()
{
    //这里和找航点是一样的，制作者需要自己不断尝试
    //找到比较合适的防御塔坑点
    QPoint pos[]=
    {
        QPoint(417,129),
        QPoint(74,240),
        QPoint(318,257),
        QPoint(277,143),
        QPoint(224,257),
        QPoint(353,363),
        QPoint(255,384),
        QPoint(506,372),
        QPoint(467,447),
        QPoint(358,484),
    };
    int len=sizeof(pos)/sizeof(pos[0]);
    for(int i=0;i<len;i++)
    {
        m_towerPositionList.push_back(pos[i]);
    }
}
void MainWindow::getHpDamaged()
{
    m_playerHp-=1;//敌人进入基地，扣一滴血
}

void MainWindow::awardGlod()
{
    m_playerGlod+=200;//杀死一个敌人，奖励200，制作者可以自由更改数值
}

void MainWindow::removeEnemy(Enemy *enemy)
{
    Q_ASSERT(enemy);
    m_enemyList.removeOne(enemy);//死亡的敌人从enemylist中移除
    delete enemy;
    if(m_enemyList.empty())
        {
            ++m_waves;
            if(!loadWaves())
            {
                m_gameWin=true;
            }
        }
}

QList<Enemy *> MainWindow::getEnemyList()
{
    return m_enemyList;
}

bool MainWindow::loadWaves()
{
    if(m_waves>=6)
    {
        return false;
    }
    int enemyStartInterval[]={100,500,600,1000,3000,6000};//敌人出现的时间
    for(int i=0;i<6;++i)
    {
        wayPoint * startWayPoint;
        startWayPoint=m_wayPointList.first();
        Enemy * enemy=new Enemy(startWayPoint,this);//创建一个新得enemy
        m_enemyList.push_back(enemy);
        QTimer::singleShot(enemyStartInterval[i],enemy,SLOT(doActive()));
    }
    return true;
}

void MainWindow::gameStart()
{
    loadWaves();
}

void MainWindow::updateMap()
{
    foreach(Enemy * enemy,m_enemyList)
        enemy->move();
    update();
}
